<template>
	<view>
		<!-- <button @tap="login">微信登录</button> -->
		<view v-if="isCanUse">
			<view>
				<view class='header'>
					<image src="/static/微信.png"></image>
				</view>
				<view class='content'>
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像、地区等)</text>
				</view>

				<!-- <button class='bottom' type='primary' open-type="getUserInfo" withCredentials="true" lang="zh_CN"
					@getuserinfo="login">
					授权登录
				</button> -->
				<button class='bottom' type='primary' @tap="login">授权登录</button>
			</view>
		</view>

		<view>
			<!-- 小程序隐私协议 -->
			<privacy-popup></privacy-popup>
		</view>


	</view>
</template>

<script>
	import PrivacyPopup from "@/components/xw-privacy-popup/privacy-popup.vue";
	import * as WxUsersApi from '@/api/wxusers';
	import {
		loginByWechat
	} from '@/api/login';
	// import Verify from "@/components/verifition/Verify"

	export default {
		// components: {
		// 	Verify
		// },
		components: {
			PrivacyPopup
		},
		data() {
			return {
				isCanUse: uni.getStorageSync('isCanUse') || true, //默认为true
				pid: 2789,
				globalConfig: getApp().globalData.config,

				userInfo: {}, // 使用对象而不是数组来存储用户信息  
				queryParams: {
					nickName: null,
					mobile: null,
					avatarUrl: null,
					openId: null,
					sessionKey: null,
					city: null,
					province: null,
					country: null,
					language: null,
					gender: null,
					spareField1: null,
					spareField2: null,
					spareField3: null,
					spareField4: null,
					spareField5: null,
					creator: null
				},
				list: [], // 用于存储查询到的用户列表（如果有需要）  
				loginForm: {
					username: "",
					password: "",
					// captchaVerification: "",
					openId: ""
				},
				loginInfo: {
					code: ""
				}
			};
		},
		onLoad() {},
		methods: {
			async login() {
				const self = this;
				uni.getUserProfile({
					desc: "获取你的昵称、头像信息",
					success: (res) => {
						const userInfo = res.userInfo;
						console.log("用户基本信息", userInfo);

						uni.login({
							success(lres) {
								console.info(lres, '请求微信登录返回的数据')
								const params = {
									code: lres.code
								}
								// this.loginByWechat(params)
								loginByWechat(params).then(res => {
									console.log(res, '微信一键登录返回的数据')
									const openId = res.openid;

									self.loginForm.openId = openId;
									self.handleLogin();


								}).catch(error => {
									console.error('登录失败:', error);
								});

							}
						})

					},
					fail: (error) => {
						console.error('获取用户信息失败:', error);
					}
				});
			},
			async handleLogin(params) {
				const app = getApp();
				app.globalData.openId = this.loginForm.openId;
				// 直接设置用户名和密码，跳过输入验证  
				this.loginForm.username = 'admin';
				this.loginForm.password = 'admin123';


				// 跳过验证码校验，直接调用 pwdLogin 方法  
				if (this.captchaEnabled) {
					// 如果在正常情况下需要验证码，但这里我们直接跳过它  
					// 可以选择不传递任何验证码参数或者传递一个空对象  
					await this.pwdLogin({});
				} else {
					// 如果本身就不需要验证码，则直接调用 pwdLogin  
					await this.pwdLogin({});
				}
			},

			// 密码登录
			async pwdLogin() {
				this.$modal.loading("登录中，请耐心等待...")
				// 执行登录
				// this.loginForm.captchaVerification = captchaParams.captchaVerification
				this.$store.dispatch('Login', this.loginForm).then(() => {
					this.$modal.closeLoading()
					this.loginSuccess()
				})
			},
			// 登录成功后，处理函数
			loginSuccess(result) {
				//this.$tab.reLaunch('/pages/index')
				// 设置用户信息
				this.$store.dispatch('GetInfo', this.pid).then(res => {
					this.$tab.reLaunch('/pages/index')

				})
			},


		}
	};
</script>

<style>
	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
</style>